package net.maku.express.biza.controller.collectExpress;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.maku.express.biza.controller.collectExpress.vo.*;
import net.maku.express.biza.service.collectExpress.CollectExpressService;
import net.maku.express.biza.vo.CollectExpressSingleVO;
import net.maku.express.biza.vo.ExpressRelatedDataVO;
import net.maku.framework.common.utils.Result;
import net.maku.framework.common.vo.IdVO;
import net.maku.framework.common.vo.StrIdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Description:
 * @Date
 */
@RequestMapping("collectExpress")
@RestController
@Tag(name = "揽件")
public class CollectExpressController {

    @Autowired
    private CollectExpressService collectExpressService;

    @PostMapping("/select")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<IPage<CollectExpressPageVO>> select(@RequestBody @Valid CollectExpressRequestVo collectExpressVO) {
        return collectExpressService.select(collectExpressVO);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    @PreAuthorize("hasAuthority('express:collect:add')")
    public Result<Integer> add(@RequestBody @Valid CollectExpressSingleVO vo) {
        return collectExpressService.addData(vo);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('express:collect:delete')")
    public Result<String> delete(@RequestBody @Valid JSONObject jsonObject) {
        Long id = jsonObject.getLong("id");
        return collectExpressService.deleteData(id);
    }


    @PutMapping("/update")
    @Operation(summary = "更新")
    @PreAuthorize("hasAuthority('express:collect:update')")
    public Result<Integer> update(@RequestBody @Valid CollectExpressSingleVO collectExpressVO) {
        return collectExpressService.updateData(collectExpressVO);
    }

//    @PostMapping("/selectSmallNo")
//    @Operation(summary = "小包裹编号")
//    @PreAuthorize("hasAuthority('express:collect:select')")
//    public Result<String> selectSmallNo(@RequestBody StrIdVO idVO) {
//        return collectExpressService.getSmallSysNo(idVO.getId());
//    }


//    @GetMapping("/selectNo")
//    @Operation(summary = "包裹编号")
//    @PreAuthorize("hasAuthority('express:collect:select')")
//    public Result<String> selectNo() {
//        return collectExpressService.getSysNo();
//    }


    @PostMapping("/selectSmall")
    @Operation(summary = "小包裹")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<List<GoodsDetailVO>> selectSmall(@RequestBody @Valid IdVO idVO) {
        return collectExpressService.selectSmall(idVO.getId());
    }


    @PostMapping("/selectOne")
    @Operation(summary = "详情")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<CollectExpressSingleVO> selectOne(@RequestBody @Valid IdVO idVO) {
        return collectExpressService.selectOne(idVO.getId());
    }

    @PostMapping("/selectOneList")
    @Operation(summary = "打印收据详情")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<List<CollectExpressSelectiveVO>> selectOneList(@RequestBody @Valid JSONObject jsonObject) {
        if (!jsonObject.containsKey("ids")) {
            return Result.error("参数有误");
        }
        List<Long> ids = JSONUtil.toList(jsonObject.getString("ids"), Long.class);
        return collectExpressService.selectOneList(ids);
    }


    @PostMapping("/selectPackList")
    @Operation(summary = "散件合包货物")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<IPage<Map>> selectPackList(@RequestBody @Valid CollectPackRequestVO query) {
        return collectExpressService.selectPack(query);
    }

    @PostMapping("/selectPackList1")
    @Operation(summary = "散件和包货物查询")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<List<CollectPackResponseVO>> selectPackList1(@RequestBody @Valid JSONObject jsonObject) {
        if (jsonObject == null || !jsonObject.containsKey("id")) {
            return Result.error("参数不能为空");
        }
        Long id = jsonObject.getLong("id");
        return collectExpressService.selectPack1(id);
    }

    @PostMapping("/selectOrder")
    @Operation(summary = "散件和包货物查询")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<List<CollectExpressVO>> selectOrder(@RequestBody @Valid CollectExpressOrderRequestVO co) {
        return collectExpressService.selectOrder(co);
    }


    @PostMapping("/selectLogistics")
    @Operation(summary = "货物信息查询")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<List<LogisticsResponseVO>> selectLogistics(@RequestBody @Valid JSONObject jsonObject) {
        if (!jsonObject.containsKey("id")) {
            return Result.error("参数有误");
        }
        Long id = jsonObject.getLong("id");
        return collectExpressService.selectLogistics(id);
    }


    @PostMapping("/addLogistics")
    @Operation(summary = "货物信息查询")
    @PreAuthorize("hasAuthority('express:collect:add')")
    public Result<Integer> addLogistics(@RequestBody @Valid LogisticsAddRequestVO addRequestVO) {
        return collectExpressService.addLogistics(addRequestVO);
    }

    @PostMapping("/deleteLogistics")
    @Operation(summary = "货物信息删除")
    @PreAuthorize("hasAuthority('express:collect:delete')")
    public Result<Integer> deleteLogistics(@RequestBody @Valid JSONObject jsonObject) {
        if (!jsonObject.containsKey("id")) {
            return Result.error("参数有误");
        }
        Long id = jsonObject.getLong("id");
        return collectExpressService.deleteLogistics(id);
    }

    @GetMapping("/getExpressRelatedData")
    @Operation(summary = "业务常用数据")
    @PreAuthorize("hasAuthority('express:collect:select')")
    public Result<ExpressRelatedDataVO> getExpressRelatedData() {

        return collectExpressService.getExpressRelatedData();
    }

}
